##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  buildDataGov.R
#  -- preliminary file that produces the va_data object for the
#     2013 Gubernatorial election data
#
##########################################################

# illustrates which variables are available in the dataset

run.data=F
if(run.data==T){      

rm(list=ls())
library(foreign)
library(stringr)

path='~/Dropbox/AGT/VA_YouGov/'
va_data=read.spss(paste(path,'Virginia_Unmatched_Working.sav',sep=''))

cnty_returns=read.csv('~/Dropbox/Seeing_Spots/unmatched/VA/VACountyResults.csv',header=F)

covs=c(
# treatment/outcome variables      
"video_time",
#"video_time2",
"videos_treat","videoagain","AGT309","AGT310",
###"AGT315_3",
"AGT308",
#"AGT313",                                                                                       
# demographic/political interest 
#"CC309a",
"newsint","pid3lean","educ","ideo5","gender","birthyr","race_pre","employ_pre","marstat","govhypo_pre",
#"ownhome",
#"milstat_5",
#"union",
#"unionhh",
"faminc",
#"phone","internethome",
#"comptype",
"votereg",
"Vote2012_pre",
#"CC316","CC317",
#"CC354","CC354b","CC354c",
"zipcode2_pre","inputstate","pew_bornagain","pew_religimp","pew_churatd","pew_prayer",
#"CC302","CC305","CC306","CC308a","CC309a","CC309b","CC309c","CC309d",
# summarized issue attitude score
#"CC320","CC321","CC322_1","CC324","CC325","CC326","CC327","CC328","CC329",
#"CC332A","CC332B","CC332C","CC332D","CC332E","CC332F","CC332G","CC332H","CC332I","CC332J",
"pid7_pre")

indx=array(NA,length(covs))
for(j in 1:length(indx)){
	ix=grep(names(va_data),pattern=covs[j])
	if(length(ix)>1){break()}
	indx[j]=ix
} 

va_data=as.data.frame(va_data)[,indx]
tr=va_data$videos_treat

tr_str=str_sub(tr,1,4) 

tr_time=array(NA,length(tr))
# updated sizes
tr_time[which(tr_str=='CN12')]=7.9875158
tr_time[which(tr_str=='CN13')]=6.8203158
tr_time[which(tr_str=='CN21')]=7.9908041
tr_time[which(tr_str=='CN23')]=7.1836041
tr_time[which(tr_str=='CN31')]=6.8225826
tr_time[which(tr_str=='CN32')]=7.1785826
tr_time[which(tr_str=='CP12')]=5.9337966
tr_time[which(tr_str=='CP13')]=5.6337966
tr_time[which(tr_str=='CP21')]=5.9286941
tr_time[which(tr_str=='CP23')]=6.0078941
tr_time[which(tr_str=='CP31')]=5.6297852
tr_time[which(tr_str=='CP32')]=6.0089852
tr_time[which(tr_str=='MN12')]=4.9130333
tr_time[which(tr_str=='MN13')]=5.0266333
tr_time[which(tr_str=='MN21')]=4.9014255
tr_time[which(tr_str=='MN23')]=3.9718255
tr_time[which(tr_str=='MN31')]=5.0178237
tr_time[which(tr_str=='MN32')]=3.9754237
tr_time[which(tr_str=='MP12')]=4.8013609
tr_time[which(tr_str=='MP13')]=4.7669609
tr_time[which(tr_str=='MP21')]=4.8078077
tr_time[which(tr_str=='MP23')]=5.5622077
tr_time[which(tr_str=='MP31')]=4.7917559
tr_time[which(tr_str=='MP32')]=5.5821559
va_data$tr_time=tr_time

load('~/Dropbox/Seeing_Spots/unmatched/VA/zip_outs.Rdata') 

save(va_data,zip_outs,cnty_returns,file='~/Dropbox/Seeing_Spots/replication/va_data_gov.Rdata') 
                   
}
#END buildDataGov.R